package logic

import (
	"context"
	"fresh-box-manage/freshrpc/freshrpc"
	"fresh-box-manage/freshrpc/internal/svc"
	"fresh-box-manage/models/mysql"
	"github.com/zeromicro/go-zero/core/logx"
)

type FreshAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFreshAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FreshAddLogic {
	return &FreshAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加
func (l *FreshAddLogic) FreshAdd(in *freshrpc.FreshAddRequest) (*freshrpc.FreshAddResponse, error) {
	// todo: add your logic here and delete this line

	fresh := mysql.Fresh{
		Name:  in.Name,
		Price: float64(in.Price),
		Num:   int(in.Num),
	}

	if err := fresh.FreshAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &freshrpc.FreshAddResponse{
		FreshId: int64(fresh.ID),
	}, nil
}
